<?php
/**
 * Created by PhpStorm.
 * User: ZD-SERVER
 * Date: 2017/7/28
 * Time: 11:25
 */

/**
 * Class request
 *
 */
class request{
    private $arr;
    private $sentTime=0;
    private $checkInterval=2;
    private $path="d:/cLOG.txt";
    private $sendInterval;
    private $times;
    public function setSentTime($sTime){
        $this->sentTime=strtotime($sTime);
    }

    private function excute(){
        include_once "myMail.class.php";
        $m=new myMail($this->arr);
        $m->Start();
        $m=null;
    }

    /**
     * request constructor.
     * @param $sTime string "2017-07-29 18:15:00"
     * @param  $arr addAddress=>收件人 Body=>内容 Subject=>主题
     */
    public function __construct($sTime,$arr,$sendInterval=60,$times=5)
    {
        $this->arr=$arr;
        $this->setSentTime($sTime);
        $this->sendInterval=$sendInterval;
        $this->times=$times;
    }

    public function start(){
        $reset=<<<reset
<?php
return 1;
reset;
        file_put_contents("config.php",$reset);
        ignore_user_abort(0);
        set_time_limit(0);

        $count=0;
        $countMail=0;
        while (true ){
            $count++;
            file_put_contents($this->path,$count."\n",FILE_APPEND);

            $now=date("y-m-d H:i:s");
            $now=strtotime($now);
            $continue= include "config.php";
            if(!$continue){
                $msg= "Stopped by someone";
                file_put_contents($this->path,$msg."\n",FILE_APPEND);
                die("<br>Stopped");
            }
            if($now>$this->sentTime){
                //发送邮件

//                var_dump( date("Y m d , H i s", $this->sentTime));
                $this->sentTime+=$this->sendInterval;
                $this->arr['Subject'].=$countMail;
//                var_dump( date("Y m d , H i s", $this->sentTime));
                $this->excute();
                $countMail++;
                if ($countMail>$this->times)
                    die("<br>Mail sent");
                file_put_contents($this->path,date("H:m:s")."\n",FILE_APPEND);
            }else{
                file_put_contents($this->path,date("H:m:s",$now)."\n".date("H:m:s",$this->sentTime)."\n",FILE_APPEND);
            }


            sleep($this->checkInterval);


        }


    }


}